package com.agileboot.medicine.controller.common;

import com.agileboot.common.core.dto.ResponseDTO;
import com.agileboot.domain.common.dto.CurrentLoginUserDTO;
import com.agileboot.domain.common.dto.TokenDTO;
import com.agileboot.domain.system.user.UserApplicationService;
import com.agileboot.domain.system.user.db.SysUserEntity;
import com.agileboot.domain.system.user.dto.UserDTO;
import com.agileboot.infrastructure.user.AuthenticationUtils;
import com.agileboot.infrastructure.user.web.SystemLoginUser;
import com.agileboot.medicine.customize.service.login.LoginService;
import com.agileboot.medicine.customize.service.login.command.LoginCommand;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.hibernate.validator.constraints.Length;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import javax.validation.constraints.Pattern;

@Tag(name = "登录用户端", description = "登录相关接口")
@RestController
@RequestMapping("/api/auth")
public class LoginUserController {
    @Autowired
    private LoginService loginService;
    @Autowired
    private UserApplicationService applicationService;

    public final static String EMAIL_REGEXP = "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$";
    public final static String USERNAME_REGEXP = "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$";

    /**
     * 登录方法
     *
     * @param loginCommand 登录信息
     * @return 结果
     */
    @Operation(summary = "登录")
    @PostMapping("/login")
    public ResponseDTO<TokenDTO> login(@RequestBody LoginCommand loginCommand) {
        // 生成令牌
        String token = loginService.login(loginCommand);
        SystemLoginUser loginUser = AuthenticationUtils.getSystemLoginUser();
        CurrentLoginUserDTO currentUserDTO = applicationService.getLoginUserInfo(loginUser);

        return ResponseDTO.ok(new TokenDTO(token, currentUserDTO));
    }




    private BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();

    @PostMapping(value = "/valid_email")
    public ResponseDTO validateEmail(@Pattern(regexp = EMAIL_REGEXP)@RequestParam("email") String email, HttpSession session) {
        return applicationService.sendValidateEmail(email, session.getId());
    }

    /**
     @Pattern(regexp = USERNAME_REGEXP) @Length(min = 2, max = 8) @RequestParam("username") String username,
     @Length(min = 6, max = 16) @RequestParam("password") String password,
     @Pattern(regexp = EMAIL_REGEXP) @RequestParam("email") String email,
     @Length(min = 6, max = 6) @RequestParam("code") String code
     */
    @PostMapping(value = "/register")
    public ResponseDTO registerUser(@Pattern(regexp = USERNAME_REGEXP) @Length(min = 2, max = 8) @RequestParam("username") String username,
                                       @Length(min = 6, max = 16) @RequestParam("password") String password,
                                       @Pattern(regexp = EMAIL_REGEXP) @RequestParam("email") String email,
                                       @Length(min = 6, max = 6) @RequestParam("code") String code,
                                       HttpSession session) {
        UserDTO sysUserEntity = new UserDTO();
        sysUserEntity.setUsername(username);
        sysUserEntity.setPassword(password);
        password = bCryptPasswordEncoder.encode(sysUserEntity.getPassword());
        sysUserEntity.setPassword(password);
        sysUserEntity.setEmail(email);
        sysUserEntity.setCode(code);
        return applicationService.validateAndRegister(sysUserEntity,session.getId());
    }

}
